#include "dvc_info_set.h"
#include "main_windw.h"
#include "Down_load_process.h"
#include "ui_main_windw.h"
#include "Timer.h"
#include "QDateTime"

uint8_t Main_windw::set_dvc_identifier(int identifier)
{
    int progress = 30;
    uint32_t h_identifier = (uint32_t)identifier;
    ui->Write_to_device_textBrowser->setText("编号写入中...");
    ui->textBrowser_megtips->clear();
    show_color_msg("设置编号...");
    ui->progressBar_download->setValue(progress);

    if (send_one_pack(DVC_dvc_identifier, (uint8_t *)&h_identifier, 4) == D_ERROR)
    {
        ui->progressBar_download->setValue(0);
        ui->Write_to_device_textBrowser->setText("编号写入失败");
        return 1;
    }

    if (wait_order() == D_DATA)
    {

        while (progress <= 100)
        {
            ui->progressBar_download->setValue(progress++);
            delay_ms(10);
        }
        ui->Write_to_device_textBrowser->setText("编号写入成功");
        return 0;
    }
    else
    {
        ui->progressBar_download->setValue(0);
        ui->Write_to_device_textBrowser->setText("编号写入失败");
        return 1;
    }
}

uint8_t Main_windw::set_dvc_mac(uint8_t *mac, uint8_t lenth)
{

    int progress = 30;
    ui->Write_to_device_textBrowser->setText("MAC写入中...");
    ui->textBrowser_megtips->clear();
    show_color_msg("设置MAC...");
    ui->progressBar_download->setValue(progress);

    if (send_one_pack(DVC_mac, mac, lenth) == D_ERROR)
    {
        ui->progressBar_download->setValue(0);
        ui->Write_to_device_textBrowser->setText("MAC写入失败");
        return 1;
    }

    if (wait_order() == D_DATA)
    {

        while (progress <= 100)
        {
            ui->progressBar_download->setValue(progress++);
            delay_ms(10);
        }
        QDateTime cur_t = QDateTime::currentDateTime();
        QString show_temp = "MAC写入成功:";
        show_temp.append(cur_t.toString("yyyy-MM-dd HH:mm:ss"));
        ui->Write_to_device_textBrowser->setText(show_temp);

        return 0;
    }
    else
    {
        ui->progressBar_download->setValue(0);
        ui->Write_to_device_textBrowser->setText("MAC写入失败");
        return 1;
    }
}

void Main_windw::send_order_to_dvc(uint8_t type, uint8_t *order, uint8_t lenth)
{

    ui->textBrowser_megtips->clear();
    show_color_msg("发送指令...");
    if (type == DVC_adjust)
    {
        send_order(order, lenth);
    }
    else if (type == DVC_make_zero)
    {
        send_order(order, lenth);
    }
    else if (type == DVC_my_find_dvc)
    {
        send_order(order, lenth);
    }
}
/*
 * 传入32-6400的周期
 */
uint8_t Main_windw::set_adv_int(int adv_int)
{

    int progress = 30;
    uint16_t h_adv = (uint16_t)adv_int;
    ui->Write_to_device_textBrowser->setText("广播间隔设置中...");
    swapEndianness(&h_adv, 2);

    ui->textBrowser_megtips->clear();
    show_color_msg("设置广播间隔...");
    ui->progressBar_download->setValue(progress);

    if (send_one_pack(DVC_adv_int, (uint8_t *)&h_adv, 2) == D_ERROR)
    {
        ui->progressBar_download->setValue(0);
        ui->Write_to_device_textBrowser->setText("广播间隔设置失败");
        return 1;
    }

    if (wait_order() == D_DATA)
    {

        while (progress <= 100)
        {
            ui->progressBar_download->setValue(progress++);
            delay_ms(10);
        }
        ui->Write_to_device_textBrowser->setText("广播间隔设置成功");
        return 0;
    }
    else
    {
        ui->progressBar_download->setValue(0);
        ui->Write_to_device_textBrowser->setText("广播间隔设置失败");
        return 1;
    }
}

uint8_t Main_windw::set_tx_power(uint8_t tx_power)
{
    int progress = 30;
    ui->Write_to_device_textBrowser->setText("发射功率设置中...");
    ui->textBrowser_megtips->clear();
    show_color_msg("设置发射功率...");
    ui->progressBar_download->setValue(progress);

    if (send_one_pack(DVC_rfpw, (uint8_t *)&tx_power, 1) == D_ERROR)
    {
        ui->progressBar_download->setValue(0);
        ui->Write_to_device_textBrowser->setText("发射功率设置失败");
        return 1;
    }

    if (wait_order() == D_DATA)
    {

        while (progress <= 100)
        {
            ui->progressBar_download->setValue(progress++);
            delay_ms(10);
        }
        ui->Write_to_device_textBrowser->setText("发射功率设置成功");
        return 0;
    }
    else
    {
        ui->progressBar_download->setValue(0);
        ui->Write_to_device_textBrowser->setText("发射功率设置失败");
        return 1;
    }
}

/**
 * @brif 同步时间
 */
uint8_t Main_windw::set_dvc_time(uint8_t *time, uint8_t lenth)
{
    int progress = 30;
    ui->Write_to_device_textBrowser->setText("时间同步中...");
    ui->textBrowser_megtips->clear();
    show_color_msg("同步日历时间...");
    ui->progressBar_download->setValue(progress);

    if (send_one_pack(DVC_my_set_local_time, time, lenth) == D_ERROR)
    {
        ui->progressBar_download->setValue(0);
        ui->Write_to_device_textBrowser->setText("时间同步失败");
        return 1;
    }

    if (wait_order() == D_DATA)
    {

        while (progress <= 100)
        {
            ui->progressBar_download->setValue(progress++);
            delay_ms(10);
        }
        QDateTime cur_t = QDateTime::currentDateTime();
        QString show_temp = "时间同步成功:";
        show_temp.append(cur_t.toString("yyyy-MM-dd HH:mm:ss"));
        ui->Write_to_device_textBrowser->setText(show_temp);

        return 0;
    }
    else
    {
        ui->progressBar_download->setValue(0);
        ui->Write_to_device_textBrowser->setText("时间同步失败");
        return 1;
    }
}

/*!
 *   @bref   下载进程入口
 */
void Main_windw::down_load_process(void)
{

    all_down_process();
}
